import {
	getPlanDetail
} from '@/apis/maintains.js';
import { hanleToken } from '@/utils/utils.js';
const parts = {
	state: {
		getDetailArr: JSON.parse(uni.getStorageSync('getDetailArr') || '[]'),
		signState:uni.getStorageSync('signState') || false,
	},
	mutations: {
		getDetailData(state, arr) {
			state.getDetailArr = arr;
			uni.setStorageSync('getDetailArr',JSON.stringify(arr || {}));
		}, 
		// 是否加入考勤
		addSign(state, status) {
			state.signState = status;
			uni.setStorageSync('signState',status);
		},
	},
	actions: {
		// 获取保养任务详情 obj 数据区分重点和普通保养
		getDetail(context,obj) {
			return new Promise((resolve, reject)=>{
				uni.showToast({
					title:'数据加载中...',
					icon:'loading'
				});
				var objData = {};
				objData = {
					id:obj.stateId,
					groupId:obj.groupId
				};
				// 重点保养不传groupId
				if (obj.akeyState) {
					delete objData.groupId; 
				}
				getPlanDetail(objData).then(res=>{
					if (res.code == 200) {
						var planList = [];
						context.commit('getDetailData', planList || []);
						// 重点保养没图时为空数组
						if (obj.akeyState) {
							let imgObj = res.data.planCheckVo;
							imgObj.imgUrl = imgObj.imgUrl ? imgObj.imgUrl : [];
							imgObj.disposeImgUrl = imgObj.disposeImgUrl ? imgObj.disposeImgUrl : [];
							planList.push(imgObj); 
							
							context.commit('getDetailData', planList || []);
						} else {
							planList = res.data.planCheckVoList;
							context.commit('getDetailData', planList || []);
						}
						hanleToken('set','testlistimg',planList);
						hanleToken('set','handArrlist',planList);
						hanleToken('set','getValTest',planList);
						hanleToken('set','getValHand',planList);
					}
					resolve(res);
				}).catch(err=>{
					reject(err);
					uni.showToast({
						title:'系统错误',
						icon:'none'
					});
				});
			});
		},
		
	}
};

export default parts;
